<?php
class TokenMngr
{
	public $token;
	public $token_inv;
	public $current_priority;
	public $listVarType;
	
	function __construct($token)
	{
		$this->token = $token;
		$this->token_inv = array();
		$this->token_priority = array();
		$this->token_association = array();
		$this->listVarType = array();
		for($i=0; $i<count($token); $i++)
		{
			$this->token_inv[$token[$i]] = $i;
			$this->token_priority[] = -1;//pas de priorite
			$this->token_association[] = 1;//right
		}
		$this->current_priority = 0;
	}
	
	public function getId($token)
	{
		if(is_array($token))
		{
			$ret = array();
			for($i=0; $i<count($token);$i++)
				$ret[] = $this->token_inv[$token[$i]];
			return $ret;
		}
		else return $this->token_inv[$token];
	}
	
	public function addTokenState($assoc, $list_token)
	{
		if($assoc == "left")
			$assoc = 0;
		else $assoc = 1;
		for($i=0; $i < count($list_token); $i++)
		{
			$id = $this->token_inv[$list_token[$i]];
			$this->token_priority[$id] = $this->current_priority;
			$this->token_association[$id] = $assoc;
		}
		$this->current_priority++;
	}
}

class TokenData
{
	public $token_id;
	public $param;
	
	function __construct($token_id)
	{
		$this->token_id = $token_id;
		$this->param = null;
	}
}

class VarParam
{
	public $type;
	public $mem;
}

class VarType
{
	public $name;
	public $heritage;
	public $variables;
	public $methods;
	
	function __construct()
	{
		$this->name = '';
		$this->heritage = array();
		$this->variables = array();
		$this->methods = array();
	}
}

class MethodParam
{
	public $name;
	public $arguments;
	public $returnType;
	
	function __construct()
	{
		$this->name = '';
		$this->arguments = array();
		$this->returnType = null;
	}
}

class TokenDataText extends TokenData
{
	public $text;
	function __construct($token_id, $text)
	{
		parent::__construct($token_id);
		$this->text = $text;
	}
	
	public function beginWith($tokens)
	{
		return count($tokens) == 0 || (count($tokens) == 1 && $tokens[0] == $this);
	}
	
	public function toString($tokenMngr)
	{
		return $tokenMngr->token[$this->token_id]."(\"".$this->text."\")";
	}
}

class TokenDataArray extends TokenData
{
	public $array;
	function __construct($token_id)
	{
		parent::__construct($token_id);
		$this->array = array();
	}
	
	public function addTokenData($tokenData)
	{
		$this->array[] = $tokenData;
	}
	
	public function beginWith($tokens)
	{
		for($i=0; $i<count($tokens) && $i<count($this->array); $i++)
			if($this->array[$i] != $tokens[$i])
				break;
		return $i == count($tokens) || (count($this->array)>0 && $this->array[0]->beginWith($tokens));
	}
	
	public function toString($tokenMngr)
	{
		$str = $tokenMngr->token[$this->token_id]."(";
		for($i=0; $i<count($this->array); $i++)
		{
			if($i>0) $str .= ",";
			$str .= $this->array[$i]->toString($tokenMngr);
		}
		return $str.")";
	}
}
?>